package com.zhenhunfan.hello.rpc.demo01;

import com.zhenhunfan.hello.rpc.common.IUserService;
import com.zhenhunfan.hello.rpc.common.User;
import com.zhenhunfan.hello.rpc.common.UserServiceImpl;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * <p>Title: Server</p>
 * <p>Description: </p>
 *
 * @author zhenhunfan
 * @version 1.0.0
 * @date 2021/4/8
 */
public class Server {
    public static boolean running = true;

    public static void main(String[] args) throws IOException {
        ServerSocket ss = new ServerSocket(9000);
        while(running){
            Socket s = ss.accept();
            process(s);
            s.close();
        }
        ss.close();
    }

    private static void process(Socket s) throws IOException {
        InputStream in = s.getInputStream();
        OutputStream out = s.getOutputStream();
        DataInputStream dis = new DataInputStream(in);
        DataOutputStream dos = new DataOutputStream(out);

        int id = dis.readInt();
        IUserService service = new UserServiceImpl();
        User user = service.findUserById(id);
        dos.writeInt(user.getId());
        dos.writeUTF(user.getName());
        dos.flush();
    }
}
